package lt.mii.optimum.io;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;


public class XmlFileOut implements Serializable{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2677193667948020019L;
	public BufferedWriter out;
	static String lastOuterTag;
	
	
	public XmlFileOut(String rootdir) {
		try {
		
		//System.out.println("pagal ideja, faila sukure : "  );
		out = new BufferedWriter(new FileWriter(rootdir+"/Default.xml"));
        out.write("<?xml version=\"1.0\"?> " +
        		  "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" " +
        		  "xmlns:o=\"urn:schemas-microsoft-com:office:office\"  " +
        		  "xmlns:x=\"urn:schemas-microsoft-com:office:excel\" " +
        		  "xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"  " +
        		  "xmlns:html=\"http://www.w3.org/TR/REC-html40\">  " +
        		  "<Styles> " +
        		  "<Style ss:ID=\"Default\" ss:Name=\"Normal\"> " +
        		  "<Alignment ss:Vertical=\"Bottom\"/> " +
        		  "<Borders/> " +
        		  "<Font/> " +
        		  "<Interior/> " +
        		  "<NumberFormat/> " +
        		  "<Protection/>  " +
        		  "</Style> " +
        		  "</Styles> " +
        		  "<Worksheet ss:Name=\"Pradinis tvarkarastis\">  " +
        		  "<Table>");       
		
        //System.out.println("\n ir kazka irashe");
		} catch (IOException ex){
			System.out.println("Klaida rasant i faila Default.xml");
		}
	}
	public XmlFileOut(String pavad, String rootdir){
		try {
		
		//System.out.println("pagal ideja, faila sukure : "  );
		out = new BufferedWriter(new FileWriter(rootdir+"/"+pavad));
        out.write("<?xml version=\"1.0\"?> " +
        		  "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" " +
        		  "xmlns:o=\"urn:schemas-microsoft-com:office:office\" " +
        		  "xmlns:x=\"urn:schemas-microsoft-com:office:excel\" " +
        		  "xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" " +
        		  "xmlns:html=\"http://www.w3.org/TR/REC-html40\"> " +
        		  "<Styles> " +
        		  "<Style ss:ID=\"Default\" ss:Name=\"Normal\"> " +
        		  "<Alignment ss:Vertical=\"Bottom\"/> " +
        		  "<Borders/> " +
        		  "<Font/> " +
        		  "<Interior/> " +
        		  "<NumberFormat/> " +
        		  "<Protection/> " +
        		  "</Style> " +
        		  "</Styles> " +
        		  "<Worksheet ss:Name=\"Rezultatai\"> " +
        		  "<Table>");       
		
        //System.out.println("\n ir kazka irashe");
		} catch (IOException ex){
			System.out.println("Klaida rasant i faila "+ pavad);
			ex.printStackTrace();
			
		}
	}
	
	public void addTag(String Data){
		lastOuterTag=Data;
		try{
		out.write("<Cell><Data ss:Type=\"String\">"+Data+"</Data></Cell>");
		} catch (IOException ex){
			System.out.println("Klaida rasant duomenys.");
		}catch (NullPointerException ex){
			System.out.println("Neimanoma rasyti i atidaryta faila. Ji naudoja kita programa"+out.toString());
			System.exit(1);
		}
	}
public void startRow(){
		
		try{
		out.write("<Row>");
		} catch (IOException ex){
			System.out.println("Eilute nepradeta");
		} catch (NullPointerException ex){
			System.out.println("Neimanoma rasyti i atidaryta faila. Ji naudoja kita programa");
			System.exit(1);
		}
	}
	public void endRow(){
		
		try{
		out.write("</Row>");
		} catch (IOException ex){
			System.out.println("Eilute nebaigta");
		}catch (NullPointerException ex){
			System.out.println("Neimanoma rasyti i atidaryta faila. Ji naudoja kita programa");
			System.exit(1);
		}
	}
	public void close(){
		try{
			out.write("  </Table></Worksheet></Workbook>");
			out.close();
			} catch (IOException ex){
				System.out.println("Nepavyko uzdaryti failo.");
			}catch (NullPointerException ex){
				System.out.println("Neimanoma rasyti i atidaryta faila. Ji naudoja kita programa");
				System.exit(1);
			}
	}
	
}
